package cn.hp.controller;

import cn.hp.entity.Dite;
import cn.hp.service.DiteService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dite")
public class DiteController {

    @Autowired
    private DiteService diteService;

    @GetMapping
    public Result find(Dite dite){
        List<Dite> diteList = diteService.selectDite(dite);
        return Result.success(diteList);
    }

    @RequestMapping("/page")
    public Result page(Dite dite, @RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "10") Integer limit){
        PageInfo<Dite> pageInfo = diteService.selectDite(dite, page, limit);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result insert(@RequestBody  Dite dite){
        int i = diteService.insertDite(dite);
        return i > 0 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @PutMapping
    public Result update(@RequestBody Dite dite){
        int i = diteService.updateDite(dite);
        return i > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int i = diteService.deleteDiteById(id);
        return i > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }


}